import { _decorator, Component, Node, Button, Vec3, tween, Tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Upgrade')
export class Upgrade extends Component {
    @property(Node)
    private cube1: Node = null; // 第一个 Cube

    @property(Node)
    private cube2: Node = null; // 第二个 Cube

    @property(Node)
    private cube3: Node = null; // 第三个 Cube

    @property(Button)
    private toggleButton: Button = null; // 按钮

    @property(Node)
    private s2: Node = null;

    @property(Node)
    private s1: Node = null;

    private currentCube: Node = null; // 当前激活的 Cube

    start() {
        //this.toggleButton.node.on('click', this.onButtonClick, this);
        this.cube2.active = false; // 初始时禁用第二个 Cube
        this.cube3.active = false; // 初始时禁用第三个 Cube
        this.currentCube = this.cube1; // 设置当前激活的 Cube
    }

    update(deltaTime: number) {
        
    }

    public async onButtonClickLine() {
        if (this.currentCube === this.cube1) {
            this.s1.active = true;
            await this.toggleCubes(this.cube1, this.cube2);
            this.currentCube = this.cube2; // 更新当前激活的 Cube
        } else if (this.currentCube === this.cube2) {
            this.s2.active = true;
            await this.toggleCubes(this.cube2, this.cube3);
            this.currentCube = this.cube3; // 更新当前激活的 Cube
        } else {
            await this.toggleCubes(this.cube3, this.cube1);
            this.currentCube = this.cube1; // 更新当前激活的 Cube
        }
    }

    private async toggleCubes(fromCube: Node, toCube: Node) {
        // 获取当前 Cube 的缩放
        const currentScale = fromCube.scale.clone();

        // 缩小当前激活的 Cube
        await this.scaleObject(fromCube, new Vec3(currentScale.x * 0.2, currentScale.y * 0.2, currentScale.z * 0.2), 0.3);
        fromCube.active = false; // 禁用当前激活的 Cube
        this.scaleObject(fromCube, currentScale, 0);

        // 激活目标 Cube
        const currentScaleTo = toCube.scale.clone();
        this.scaleObject(toCube, new Vec3(currentScale.x * 0.2, currentScale.y * 0.2, currentScale.z * 0.2), 0);
        toCube.active = true;
        // 放大目标 Cube
        await this.scaleObject(toCube, currentScaleTo, 0.3);
    }

    private scaleObject(obj: Node, targetScale: Vec3, duration: number): Promise<void> {
        return new Promise<void>((resolve) => {
            tween(obj)
                .to(duration, { scale: targetScale }, { easing: 'sineInOut' })
                .call(() => resolve())
                .start();
        });
    }
}


